package com.rain.cy.business.model.dto;

import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import com.rain.cy.business.constant.CountTypeEnum;
import com.rain.cy.business.model.entity.DictionaryEntity;
import lombok.Data;
import org.assertj.core.util.Lists;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * <p>
 * 统计数据的json
 * <p>
 * <p>
 * xAxis: {
 * type: 'category',
 * boundaryGap: false,
 * data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
 * },
 */
@Data
public class CountDataDTO {

    /**
     * 当前条目平均分
     */
    private Integer scoreAvg;

    private String type;

    /**
     * 展示的文案
     */
    private List<String> legendList;
    /**
     * 横坐标 name 日期
     */
    private List<String> xTypeList;
    /**
     * 横坐标数据
     */
    private List<SeriesItem> seriesList;

    private Map<String, SeriesItem> seriesItemMap;


    /**
     * {
     * name: 'Email',
     * type: 'line',
     * stack: 'Total',
     * data: [120, 132, 101, 134, 90, 230, 210]
     * }
     */
    @Data
    public static class SeriesItem {


        private String name;
        /**
         * see#com.rain.cy.business.constant.CountTypeEnum
         */
        private String type;

        private List<Integer> data;

        public static SeriesItem of(String name, String type) {
            SeriesItem seriesItem = new SeriesItem();
            seriesItem.setName(name);
            seriesItem.setData(Lists.newArrayList());
            seriesItem.setType(type);
            return seriesItem;
        }

    }


    public void appendLegend(String legend) {
        SeriesItem seriesItem = SeriesItem.of(legend, this.type);
        this.legendList.add(legend);
        this.seriesList.add(seriesItem);
        this.seriesItemMap.put(legend, seriesItem);
    }

    public static CountDataDTO of() {
        CountDataDTO countDataDTO = new CountDataDTO();
        countDataDTO.setLegendList(Lists.newArrayList());
        countDataDTO.setSeriesList(Lists.newArrayList());
        countDataDTO.setSeriesItemMap(Maps.newHashMap());
        countDataDTO.setXTypeList(Lists.newArrayList());
        return countDataDTO;
    }

    public static CountDataDTO of(List<DictionaryEntity> legendList) {
        CountDataDTO countDataDTO = CountDataDTO.of();
        countDataDTO.setType(CountTypeEnum.BAR.getType());
        if (CollectionUtil.isNotEmpty(legendList)) {
            for (DictionaryEntity s : legendList) {
                countDataDTO.appendLegend(s.getItemValue());
            }
        }
        return countDataDTO;
    }

    public static CountDataDTO of(List<DictionaryEntity> legendList, String type) {
        CountDataDTO countDataDTO = of(legendList);
        countDataDTO.setType(type);
        return countDataDTO;
    }

    public CountDataDTO xTypeList(List<DictionaryEntity> xTypeList) {
        this.xTypeList = xTypeList.stream().map(DictionaryEntity::getItemValue).collect(Collectors.toList());
        return this;
    }

    public CountDataDTO xTypeStrList(List<String> xTypeList) {
        this.xTypeList = xTypeList;
        return this;
    }

    public CountDataDTO legendList(List<String> legendList) {
        this.legendList = legendList;
        return this;
    }
}
